var totalCount = 0;
var pageSize = 5;
var currentPageIndex = 0;
var sortItem = null;
var sortIDirection = "desc";
$(document).ready(function () {

    $("#dataTable thead th").click(function () {
        sortItem = $(this).attr("sort-data");
        ResetSortDirection($(this), sortItem);
        currentPageIndex = 0;
        LoadData(currentPageIndex);
    });

    $(".grid-pager a").click(function (event) {

        var isValidPage = true;
        var totalPage = Math.ceil(totalCount / pageSize);
        var pageitem = $(this).attr("data-page");
        if (pageitem == "first") {
            currentPageIndex = 0;
        }
        else if (pageitem == "prev") {
            if (currentPageIndex + 1 > 1) {
                currentPageIndex = currentPageIndex - 1;
            }
            else {
                isValidPage = false;
            }
        }
        else if (pageitem == "next") {

            if (currentPageIndex + 1 < totalPage) {
                currentPageIndex = currentPageIndex + 1;
            }
            else {
                isValidPage = false;
            }
        }
        else if (pageitem == "last") {
            currentPageIndex = totalPage - 1;
        }
        else {
            // Do Nothing
        }

        if (isValidPage == true) {
            SetPager(currentPageIndex);
            LoadData(currentPageIndex);
            ResetButtonStyles(currentPageIndex, totalPage);
        }
        else {
            // Do nothing
        }

        
        return false;
    });

    ResetPager();
    LoadPager();
    LoadData(0);

});

function ResetButtonStyles(currentPageIndex, pageSize) {
   
    if (currentPageIndex+1 == pageSize) {
        $("a.pager-last").removeClass('button small blue pager-last').addClass('button small gray pager-last');
    }
    else {
        $("a.pager-last").removeClass('button small gray pager-last').addClass('button small blue pager-last');
    }

    if (currentPageIndex + 1 == pageSize) {
        $("a.pager-next").removeClass('button small green pager-next').addClass('button small gray pager-next');
    }
    else {
        $("a.pager-next").removeClass('button small gray pager-next').addClass('button small green pager-next');
    }

    if (currentPageIndex == 0) {
        $("a.pager-first").removeClass('button small blue pager-first').addClass('button small gray pager-first');
    }
    else {
        $("a.pager-first").removeClass('button small gray pager-first').addClass('button small blue pager-first');
    }

    if (currentPageIndex - 1 < 0) {
        $("a.pager-prev").removeClass('button small green pager-prev').addClass('button small gray pager-prev');
    }
    else {
        $("a.pager-prev").removeClass('button small gray pager-prev').addClass('button small green pager-prev');
    }

}
function ResetSortDirection(component, item) {

    if (item == sortItem) 
    {
        if (sortIDirection == "desc") 
        {
            sortIDirection = "asc";
            component.attr("class", "grid-header-desc");
        }
        else 
        {
            sortIDirection = "desc";
            component.attr("class", "grid-header-asc");
        }
    }
}

function ResetPager() 
{
    if (sortItem == null) 
    {
        sortItem = $("#dataTable thead th:first").attr("sort-data");
        sortIDirection = "desc";
    }
}

function SetPager(currentIndex) 
{
    $(".totalRecords").text(totalCount);
    $(".current").text(currentIndex + 1);
    var totalPage = Math.ceil(totalCount / pageSize);
    $(".total").text(totalPage);

}